
$(document).ready(function () {
    showList();
});


function showList() {
    $.ajax({
        type:"GET",
        dataType:"json",
        url:"rest/student/list",
        success:function(json){
            var listUser = json.data;
            alert("listUser"+listUser);
            if (listUser.length==0){
                //没有数据
                $("#listTable").html("数据库没有数据");
            }else{
                var listUserHtml =
                    "<tr>" +
                    "<th>&nbsp;</th>" +
                    "<th>student</th>" +
                    "<th>studentName</th>" +
                    "<th>studentClass</th>" +
                    "<th>操&nbsp;&nbsp;作</th>" +
                    "</tr>";
                //遍历取值
                $.each(listUser,function (i,n) {
                    i+=1;
                    listUserHtml = listUserHtml+"<tr>" +
                        "<td>"+i+"</td>" +
                        "<td>"+ n.studentId+"</td>" +
                        "<td>"+ n.studentName+"</td>" +
                        "<td>" + n.studentClass+"</td>" +
                        "<td>" +
                        "<input type='button' value='修改' class='editBtn' data-studentId='" +                                n.studentId + "' data-studentName='" + n.studentName +                                                  "'data-studentClass='" +n.studentClass + "' />" +
                        "<input type='button' value='删除' class='deleteBtn'  data-studentId='" +                                 n.studentId + "' />" +
                        "</td>" +
                        "" + "</tr>"
                })

                //让遍历的数据显示在网页上
                $("#listTable").html(listUserHtml);

                //点击修改按钮执行方法，该方法是获取原用户信息通过url传值实现页面跳转
                $(".editBtn").click(function (){
                    updateUser($(this).attr("data-studentId"),
                        $(this).attr("data-studentName"),
                        $(this).attr("data-studentClass"));
                })

                //点击删除按钮执行删除方法，并将该学生的id传入
                $(".deleteBtn").click(function(){
                    deleteUserById($(this).attr("data-studentId"));
                })
            }
        }
    })
}



        //对应上面的点击修改执行的方法
        function updateUser(studentId,studentName,studentClass){
            window.location.href="rest/student/updateUserStudent?" +
                "studentId="+studentId+"&studentName="+studentName+"&studentClass="+studentClass;
        }



        //对应上面的点击删除执行的方法
        function deleteUserById(studentId){
            $.ajax ({
                type:"GET",
                dataType:"json",
                url:"rest/student/delete",
                data:{
                    studentId: studentId
                },
                success:function(){
                    showList();
                }
            })
        }



        /*通过id查找所有学生方法*/
        function findUser(){
            $.ajax({
                type: "GET",
                dataType: "json",
                url: "rest/student/find",
                data: {
                    studentId: $("#studentId").val()
                },
                success: function(json) {
                    var obj = json.data;
                    if(obj==null){
                        $("#rs").html("没有该学生记录！");
                    }
                    else{
                        var str= "<p>studentId:"+obj.studentId+"&nbsp;&nbsp;" +
                            "studentName:"+obj.studentName+"&nbsp;&nbsp;" +
                            "studentClass:"+obj.studentClass+"</p>";
                        $("#rs").html(str);
                    }
                }
            });
        }



        //对应上面的点击添加执行的方法
        $("#saveBtn").click(function () {
            window.location.href="rest/student/addUserStudent";
        });




















